package com.itheima.ydd.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.ConfigDto;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysUserMapper;
import com.itheima.ydd.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private YddCateService yddCateService;

    @Autowired
    private YddTagService yddTagService;

    @Autowired
    private YddLinkService yddLinkService;

    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private SysConfigService sysConfigService;

    @Override
    public Map getInfo(String userId) {
        //查询用户基本信息
        SysUser userInfo = this.getById(userId);
        //查询用户角色信息
        String roleId = userInfo.getRoleId();
        List<String> roleIds = JSON.parseObject(roleId, List.class);

        LambdaQueryWrapper<SysRole> sysRoleWrapper = new LambdaQueryWrapper<>();
        sysRoleWrapper.in(SysRole::getId, roleIds); //切割出给定的字符串
        //查出对应的角色详情
        List<SysRole> sysRoles = sysRoleService.list(sysRoleWrapper);
        //封装对应的信息
        SysUserDto sysUserDto = new SysUserDto();
        BeanUtils.copyProperties(userInfo, sysUserDto);
        ArrayList<String> list1 = new ArrayList<>();
//        list1.add(sysRole.getName());
        for (SysRole sysRole : sysRoles) {
            list1.add(sysRole.getName());
        }
        sysUserDto.setRolename(list1);


        //获取菜单详情信息
        LambdaQueryWrapper<SysRoleMenu> roleMenuWrapper = new LambdaQueryWrapper<>();
        roleMenuWrapper.in(SysRoleMenu::getRoleId,roleIds);
        List<SysRoleMenu> roleMenus = sysRoleMenuService.list(roleMenuWrapper);
        List<String> menu_ids = new ArrayList<>();
        for (SysRoleMenu roleMenu : roleMenus) {
            menu_ids.add(roleMenu.getMenuId());
        }

        LambdaQueryWrapper<SysMenu> menuWrapper = new LambdaQueryWrapper<>();
        menuWrapper.in(SysMenu::getId , menu_ids);
        menuWrapper.ne(SysMenu::getPath, "");//获取到所有路径不为空的值
        menuWrapper.ne(SysMenu::getDelFlag , "1"); //获取所有未删除的
        List<SysMenu> menuList = sysMenuService.list(menuWrapper);

        //获取dictsList
        //列名为键 -
        LambdaQueryWrapper<SysDict> dictWrapper = new LambdaQueryWrapper<>();
        dictWrapper.select(SysDict::getType);
        //依据类型分类查询
        dictWrapper.groupBy(SysDict::getType);
        List<Object> dictTypes = sysDictService.listObjs(dictWrapper);
        Map<String, Map<String, String>> dictMaps = new HashMap<>();
        for (Object dictType : dictTypes) {
            //创建集合，存放数据
            LambdaQueryWrapper<SysDict> lqwDict = new LambdaQueryWrapper<>();
            lqwDict.eq(SysDict::getType, dictType);
            List<SysDict> list = sysDictService.list(lqwDict);
            Map<String, String> map = new HashMap<>();
            for (SysDict sysDict : list) {
                map.put(sysDict.getValue(), sysDict.getLabel());
            }

            dictMaps.put((String) dictType, map);

        }

//        Map<Integer , String> btnList = new HashMap<>();
//        btnList.put(0,"*");
        ArrayList<String> btnList = new ArrayList<>();
        btnList.add(0, "*");

        //存放数据并返回
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo", sysUserDto);
        map.put("menuList", menuList);
        map.put("dictList", dictMaps);
        map.put("btnList", btnList);

        return map;
    }

    /**
     * 前台首页和全局数据
     *
     * @return
     */
    @Override
    public Map index() {
        Map<String, Object> allInfo = new HashMap<>();
        //查询所有cate信息
        List<YddCate> allCate = yddCateService.list();
        allInfo.put("allCate", allCate);
        //查询所有Link信息
        List<YddLink> allLink = yddLinkService.list();
        allInfo.put("allLink", allLink);
        //查询所有Tag信息
        List<YddTag> allTag = yddTagService.list();
        allInfo.put("allTag", allTag);
        //查询Banner信息
        //banner
        LambdaQueryWrapper<YddAdvimg> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(YddAdvimg::getAdvimgpos, 15);
        List<YddAdvimg> banner = yddAdvimgService.list(lqw1);
        allInfo.put("banner", banner);
        //sideBanner
        LambdaQueryWrapper<YddAdvimg> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(YddAdvimg::getAdvimgpos, 14);
        List<YddAdvimg> sideBanner = yddAdvimgService.list(lqw1);
        allInfo.put("sideBanner", sideBanner);
        //showBanner
        LambdaQueryWrapper<YddAdvimg> lqw3 = new LambdaQueryWrapper<>();
        lqw3.ne(YddAdvimg::getAdvimgpos, 14);
        lqw3.ne(YddAdvimg::getAdvimgpos, 15);
        List<YddAdvimg> showBanner = yddAdvimgService.list(lqw1);
        allInfo.put("showBanner", showBanner);
        //allData
        List<SysConfig> allData = sysConfigService.list();
        allInfo.put("allData", allData);

        //Config
        List<List<ConfigDto>> list = new ArrayList<>();

        //获取基础配置
        LambdaQueryWrapper<SysConfig> configWrapper1 = new LambdaQueryWrapper<>();
        configWrapper1.eq(SysConfig::getTabValue, "base");
        List<SysConfig> list1 = sysConfigService.list(configWrapper1);
        List<ConfigDto> dto1 = new ArrayList<>();
        dto1 = list1.stream().map(item ->{
            ConfigDto configDto = new ConfigDto();
            BeanUtils.copyProperties(item,configDto);
            configDto.setName("基础配置");
            configDto.setKey(item.getTabValue());
            LambdaQueryWrapper<SysConfig> config1 = new LambdaQueryWrapper<>();
            config1.eq(SysConfig::getGroupValue , "站点配置");
            List<SysConfig> sysConfigs = sysConfigService.list(config1);
            Map<String ,Object> map1 = new HashMap<>();
            map1.put("name" , "站点配置");
            map1.put("data",sysConfigs);

            LambdaQueryWrapper<SysConfig> config2 = new LambdaQueryWrapper<>();
            config2.eq(SysConfig::getGroupValue , "底部信息");
            List<SysConfig> sysConfigs2 = sysConfigService.list(config1);
            Map<String ,Object> map2 = new HashMap<>();
            map2.put("name" , "底部信息");
            map2.put("data",sysConfigs2);


            LambdaQueryWrapper<SysConfig> config3 = new LambdaQueryWrapper<>();
            config3.eq(SysConfig::getGroupValue , "其他配置");
            List<SysConfig> sysConfigs3 = sysConfigService.list(config1);
            Map<String ,Object> map3 = new HashMap<>();
            map3.put("name" , "其他配置");
            map3.put("data",sysConfigs3);


            List<Map<String , Object>> children = new ArrayList<>();
            children.add(map1);
            children.add(map2);
            children.add(map3);
            configDto.setChildren(children);
            return configDto;
        }).collect(Collectors.toList());

        //获取系统配置
        LambdaQueryWrapper<SysConfig> configWrapper2 = new LambdaQueryWrapper<>();
        configWrapper2.eq(SysConfig::getTabValue, "system");
        List<SysConfig> list2 = sysConfigService.list(configWrapper2);
        List<ConfigDto> dto2 = new ArrayList<>();
        dto2 = list2.stream().map(item ->{
            ConfigDto configDto = new ConfigDto();
            BeanUtils.copyProperties(item,configDto);
            configDto.setName("系统配置");
            configDto.setKey(item.getTabValue());
            LambdaQueryWrapper<SysConfig> config1 = new LambdaQueryWrapper<>();
            config1.eq(SysConfig::getGroupValue , "管理系统配置");
            List<SysConfig> sysConfigs = sysConfigService.list(config1);
            Map<String ,Object> map1 = new HashMap<>();
            map1.put("name" , "管理系统配置");
            map1.put("data",sysConfigs);


            List<Map<String , Object>> children = new ArrayList<>();
            children.add(map1);
            configDto.setChildren(children);
            return configDto;
        }).collect(Collectors.toList());


        //获取其他配置
        LambdaQueryWrapper<SysConfig> configWrapper3 = new LambdaQueryWrapper<>();
        configWrapper3.eq(SysConfig::getTabValue, "custom");
        List<SysConfig> list3 = sysConfigService.list(configWrapper3);
        List<ConfigDto> dto3 = new ArrayList<>();
        dto3 = list3.stream().map(item ->{
            ConfigDto configDto = new ConfigDto();
            BeanUtils.copyProperties(item,configDto);
            configDto.setName("其他设置");
            configDto.setKey(item.getTabValue());
            LambdaQueryWrapper<SysConfig> config1 = new LambdaQueryWrapper<>();
            config1.eq(SysConfig::getGroupValue , "通知公告");
            List<SysConfig> sysConfigs = sysConfigService.list(config1);
            Map<String ,Object> map1 = new HashMap<>();
            map1.put("name" , "通知公告");
            map1.put("data",sysConfigs);

            LambdaQueryWrapper<SysConfig> config2 = new LambdaQueryWrapper<>();
            config2.eq(SysConfig::getGroupValue , "前台图片");
            List<SysConfig> sysConfigs2 = sysConfigService.list(config2);
            Map<String ,Object> map2 = new HashMap<>();
            map1.put("name" , "前台图片");
            map1.put("data",sysConfigs2);

            List<Map<String , Object>> children = new ArrayList<>();
            children.add(map1);
            children.add(map2);
            configDto.setChildren(children);
            return configDto;
        }).collect(Collectors.toList());

        list.add(dto1);
        list.add(dto2);
        list.add(dto3);

        Map<String , Object> mapList = new HashMap<>();
        mapList.put("list" , list);

        allInfo.put("config" ,mapList);
        return allInfo;
    }


}
